package com.cynteka.cyfoman.ui.collection.factory;

import com.cynteka.cyfoman.core.collection.CollectionIdProvider;
import com.cynteka.cyfoman.ui.TableValueProvider;
import com.cynteka.cyfoman.ui.collection.CollectionTable;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.impl.ContextBase;
import org.jdesktop.swingx.JXTable;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Sergey
 * Date: 04.08.11
 * Time: 18:24
 */
public class GridCollectionFactory extends AbstractCollectionWidgetFactory {

    @Override
    protected JXTable createTable() {
        final CollectionTable res = new CollectionTable();

        final ListSelectionListener listener = new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e2) {
                TableModel mdl = res.getModel();
                initCatalog();
                if(getCatalog() != null && mdl instanceof CollectionIdProvider && mdl instanceof TableValueProvider) {
                    TableValueProvider tableValueProvider = (TableValueProvider) mdl;
                    CollectionIdProvider collectionIdProvider = (CollectionIdProvider) mdl;
                    // TODO redesign
                    Command cmd = getCatalog().getCommand(SELECTION_CHANGED_CMD);
                    if(cmd != null) {
                        Map map = new HashMap();
                        map.put(COLLECTION_ID_PARAM, collectionIdProvider.getCollectionId());

                        List lst = new ArrayList();
                        for(int row = res.getSelectionModel().getMinSelectionIndex(); row <= res.getSelectionModel().getMaxSelectionIndex(); row++) {
                            int mdlRowIdx = res.convertRowIndexToModel(row);
                            Object o = tableValueProvider.getValue(mdlRowIdx);
                            lst.add(o);
                        }
                        map.put(SELECTED_OBJECTS_PARAM, lst);

                        ContextBase ctx = new ContextBase(map);

                        try {
                            cmd.execute(ctx);
                        } catch (Exception e1) {
                            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                            // TODO add logging
                        }
                    }
                }
            }
        };
        res.getSelectionModel().addListSelectionListener(listener);

        ComponentListener componentListener = new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                listener.valueChanged(null);
            }
        };
        res.addComponentListener(componentListener);
        return res;
    }

}
